﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microhard.Core.DBUtility;
using Microhard.Core.Algorithm;
using Microhard.JHWorkflowTrans;
using Microhard.Permission.MVCFilter;
using Microhard.Core;

namespace Microhard.Workflow
{
    public class StartJHWorkflowTransFilter : ActionFilterAttribute
    {
        private string _workflowCode;
        private Type _idj;
        private Type _config;

        public StartJHWorkflowTransFilter(string workFlowCode, Type IDJ, Type config)
        {
            _workflowCode = workFlowCode;
            _idj = IDJ;
            _config = config;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!filterContext.ActionParameters.Keys.Contains("identity"))
            {
                this.RedirectToPage(filterContext, "缺少Identity");
            }
            if (!filterContext.ActionParameters.Keys.Contains("approvers"))
            {
                this.RedirectToPage(filterContext, "缺少approvers");
            }
            if (!filterContext.ActionParameters.Keys.Contains("isWorkflowStarted"))
            {
                this.RedirectToPage(filterContext, "缺少isWorkflowStarted");
            }
            filterContext.ActionParameters["isWorkflowStarted"] = false;
            //获得datapermission参数
            var businessPKID = Guid.Parse(filterContext.ActionParameters["identity"].ToString());
            var approvers = filterContext.ActionParameters["approvers"].ToString();
            //发起流程
            JHWorkflowImpl flow = new JHWorkflowImpl();
            ITransDataInDJ idjface = Activator.CreateInstance(_idj) as ITransDataInDJ;
            IGetC6WorkflowConfig iconfig = Activator.CreateInstance(_config) as IGetC6WorkflowConfig;
            var user = MVCFilterAtrForLogon.GetLogonUser();
            ExecutedResult rtn = new ExecutedResult();
            try
            {
                rtn = flow.Add(user.UserName, _workflowCode, idjface, iconfig, businessPKID, approvers);
                filterContext.ActionParameters["isWorkflowStarted"] = rtn.IsSucceed;
            }
            catch (Exception ex)
            {
                Log.Error("StartJHWorkflowTransFilter", "发起流程失败," + user.UserName + "," + _workflowCode + "," + businessPKID);
                Log.Error("StartJHWorkflowTransFilter-Error", ex);
                this.RedirectToPage(filterContext, ex.Message);
            }
            if (!rtn.IsSucceed)
            {
                this.RedirectToPage(filterContext, rtn.ErrorMsgTxt);
            }
        }

        public void RedirectToPage(ActionExecutingContext filterContext, string code)
        {
            var ad = new ViewResult();
            ad.ViewName = "WorkflowStartError";
            ad.ViewData["errorCode"] = code;
            filterContext.Result = ad;
        }
    }
}
